package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.MallOrderDeliverDTO;
import com.cheer.dto.MallOrderQueryDTO;
import com.cheer.dto.MallOrderRefundDTO;
import com.cheer.entity.MallOrderEntity;
import com.cheer.exception.ServiceException;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.MallOrderService;
import com.cheer.vo.MallOrderInfoVO;
import com.cheer.vo.MallOrderListVO;
import com.cheer.vo.MallRefundInfoVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/order")
public class MallOrderController {

    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @ApiOperation("查询订单列表")
    @PostMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<MallOrderListVO>> list(@RequestBody MallOrderQueryDTO orderQueryDTO) {
        return Result.success(mallOrderService.listByQueryCondition(orderQueryDTO));
    }

    @ApiOperation("商户查询订单列表")
    @PostMapping("/list/shop")
    public Result<Page<MallOrderListVO>> listByShop(@RequestBody MallOrderQueryDTO orderQueryDTO) {
        if (userInfoHolder.getShopId() == null) {
            return Result.success();
        }
        orderQueryDTO.setShopId(userInfoHolder.getShopId());
        return Result.success(mallOrderService.listByQueryCondition(orderQueryDTO));
    }

    @ApiOperation("下载订单列表")
    @PostMapping("/download")
    public Result download(@RequestBody MallOrderQueryDTO orderQueryDTO, HttpServletResponse response) {
        if (userInfoHolder.getShopId() != null) {
            orderQueryDTO.setShopId(userInfoHolder.getShopId());
        }
        mallOrderService.download(orderQueryDTO, response);
        return Result.success();
    }

    @ApiOperation("导入订单")
    @PostMapping("/importOrder")
    public Result importOrder(MultipartFile file) {
        mallOrderService.importOrder(file);
        return Result.success();
    }

    @ApiOperation("导入订单快递单号")
    @PostMapping("/importOrderExpress")
    public Result importOrderExpress(MultipartFile file) {
        mallOrderService.importOrderExpress(file);
        return Result.success();
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/info")
    public Result<MallOrderInfoVO> info(@RequestParam Long id) {
        return Result.success(mallOrderService.info(id));
    }

    @ApiOperation("发货")
    @PostMapping("/deliver")
    public Result deliver(@RequestBody @Valid MallOrderDeliverDTO orderDeliverDTO) {
        MallOrderEntity order = getOrder(orderDeliverDTO.getId());
        mallOrderService.deliver(orderDeliverDTO, order);
        return Result.success();
    }

    @ApiOperation("查看退款信息")
    @GetMapping("/refund/info")
    public Result<MallRefundInfoVO> refundInfo(@RequestParam Long id) {
        return Result.success(mallOrderService.getRefundInfo(id));
    }

    @ApiOperation("同意退款")
    @PostMapping("/refund/agree")
    public Result agree(@RequestBody @Valid MallOrderRefundDTO orderRefundDTO) {
        MallOrderEntity order = getOrder(orderRefundDTO.getId());
        mallOrderService.agreeRefund(order);
        return Result.success();
    }

    @ApiOperation("拒绝退款")
    @PostMapping("/refund/reject")
    public Result reject(@RequestBody @Valid MallOrderRefundDTO orderRefundDTO) {
        MallOrderEntity order = getOrder(orderRefundDTO.getId());
        mallOrderService.rejectRefund(order, orderRefundDTO.getReply());
        return Result.success();
    }

    private MallOrderEntity getOrder(Long id) {
        MallOrderEntity order = mallOrderService.getById(id);
        if (order == null) {
            throw new ServiceException("订单不存在");
        }

        if (!order.getShopId().equals(userInfoHolder.getShopId())) {
            throw new ServiceException("没有操作该订单的权限");
        }
        return order;
    }

}
